package jframe.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import jframe.model.Blogroll;
import jframe.validator.BlogrollSaveValidator;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

import java.util.List;

/**
 * User: Ulric Qin
 * Mail: ulric.qin@gmail.com
 */
public class BlogrollController extends Controller {

    public void saveUI() {
        Long id = getParaToLong("id", 0L);
        if (id.longValue() != 0) {
            Blogroll blogroll = Blogroll.oneRecord(id);
            if (blogroll != null) {
                setAttr("o", blogroll);
            }
        }
        render("save.ftl");
    }

    public void listUI() {
        List<Blogroll> list = Blogroll.list();
        setAttr("list", list);
        render("list.ftl");
    }

    @Before(BlogrollSaveValidator.class)
    public void save() {
        String title = getPara("title").trim();
        String url = getPara("url").trim();
        int weight = getParaToInt("weight", 0);
        int id = getParaToInt("id");

        title = Jsoup.clean(title, Whitelist.none());
        url = Jsoup.clean(url, Whitelist.none());
        Blogroll.save(title, url, weight);

        setAttr("msg", "");
        renderJson();
    }

}
